package com.guoguo.base;

import com.github.pagehelper.Page;

import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2017/8/14 0014.
 */
public class PageBean<T> implements Serializable {

    private static final long serialVersionUID = 8656597559014685635L;
    private long total;        //总记录数
    private List<T> list;//结果集

    private int pageNum;    // 第几页
    private long pageSize;    // 每页记录数
    private long pages;        // 总页数
    private long size;// 当前页的数量 <= pageSize，该属性来自ArrayList的size属性



    /**
     * 包装Page对象，因为直接返回Page对象，在JSON处理以及其他情况下会被当成List来处理，
     * 而出现一些问题。
     *
     * @param list page结果
     */
    public PageBean(List<T> list) {
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
        }
    }

    public PageBean() {

    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {

        this.total = total;

    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {

        this.pageNum = pageNum;

    }

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(long pageSize) {

        this.pageSize = pageSize;
        if(pageSize<total){
            this.pageSize = pageSize;
        }else{
            pageNum=1;
        }
    }

    public long getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

}
